package com.cs.contract.controller;

import com.cs.contract.service.CsSupplierService;
import com.cs.contract.domain.CsSupplier;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.SysFile;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author：zrx
 * @Package：com.cs.cscontract.controller
 * @Project：RuoYi-Cloud
 * @name：CsSupplierController
 * @Date：2024/11/21 14:48
 * @Filename：CsSupplierController
 */
@RestController
@RequestMapping("/supplier")
public class CsSupplierController extends BaseController {
    private final RemoteFileService remoteFileService;
    private final CsSupplierService csSupplierService;

    public CsSupplierController(RemoteFileService remoteFileService, CsSupplierService csSupplierService) {
        this.remoteFileService = remoteFileService;
        this.csSupplierService = csSupplierService;
    }

    @PostMapping("/upload1")
    public AjaxResult upload1(MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
                return AjaxResult.error("文件服务异常，请联系管理员");
            }
            System.out.println("文件上传成功！。。。。");
            System.out.println("文件信息："+fileResult.getData());
            return AjaxResult.success(fileResult.getData());
        }
        return AjaxResult.error("上传文件异常，请联系管理员");
    }

    @GetMapping(value = "/{hid}")
    public AjaxResult getInfo(@PathVariable("hid") Integer hid) {
        CsSupplier csSupplier = csSupplierService.getCsSupplierByHid(hid);
        String copy = csSupplier.gethJuridicalCopies();
        if (copy != null) {
            String[] arr = copy.split(",");
            csSupplier.setIdCardCopies(arr);
        }
        return success(csSupplier);
    }

    @GetMapping("/list")
    public TableDataInfo listCsSupplierList(CsSupplier csSupplier) {
        startPage();
        List<CsSupplier> list = csSupplierService.listCsSupplierList(csSupplier);
        return getDataTable(list);
    }
}
